﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AuthoSYS.EF;

namespace AuthoSYS.Web.Controllers
{
    public class ModuleController : Controller
    {
        private AuthoDBEntities db = new AuthoDBEntities();

        //
        // GET: /Module/

        public ActionResult Index(string appId)
        {
            var modules = db.Modules.Include("Application").Where(m => m.AppId == appId);
            ViewBag.AppId = appId;
            return View(modules.ToList());
        }

        //
        // GET: /Module/Details/5

        public ActionResult Details(string id = null)
        {
            Module module = db.Modules.Single(m => m.Id == id);
            if (module == null)
            {
                return HttpNotFound();
            }
            return View(module);
        }

        //
        // GET: /Module/Create

        public ActionResult Create(string appId)
        {
            ViewBag.AppId = new SelectList(db.Applications, "Id", "ApplicationName", appId);
            Module module = new Module { Id = Guid.NewGuid().ToString(), AppId = appId };
            return View(module);
        }

        //
        // POST: /Module/Create

        [HttpPost]
        public ActionResult Create(Module module)
        {
            if (ModelState.IsValid)
            {
                db.Modules.AddObject(module);

                module.Id = Guid.NewGuid().ToString();
                module.CreatedBy = string.Empty;
                module.CreatedOn = DateTime.Now;
                module.ModifiedOn = DateTime.Now;
                module.ModifiedBy = string.Empty;

                db.SaveChanges();
                return RedirectToAction("Index", new { appId = module.AppId });
            }

            ViewBag.AppId = new SelectList(db.Applications, "Id", "ApplicationName", module.AppId);
            return View(module);
        }

        //
        // GET: /Module/Edit/5

        public ActionResult Edit(string id = null)
        {
            Module module = db.Modules.Single(m => m.Id == id);
            if (module == null)
            {
                return HttpNotFound();
            }
            ViewBag.AppId = new SelectList(db.Applications, "Id", "ApplicationName", module.AppId);
            return View(module);
        }

        //
        // POST: /Module/Edit/5

        [HttpPost]
        public ActionResult Edit(Module module)
        {
            if (ModelState.IsValid)
            {
                db.Modules.Attach(module);

                module.ModifiedOn = DateTime.Now;
                module.ModifiedBy = string.Empty;

                db.ObjectStateManager.ChangeObjectState(module, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index", new { appId = module.AppId });
            }
            ViewBag.AppId = new SelectList(db.Applications, "Id", "ApplicationName", module.AppId);
            return View(module);
        }

        //
        // GET: /Module/Delete/5

        public ActionResult Delete(string id = null)
        {
            Module module = db.Modules.Single(m => m.Id == id);
            if (module == null)
            {
                return HttpNotFound();
            }
            return View(module);
        }

        //
        // POST: /Module/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            Module module = db.Modules.Single(m => m.Id == id);
            db.Modules.DeleteObject(module);
            db.SaveChanges();
            return RedirectToAction("Index", new { appId = module.AppId });
        }

        public ActionResult GetModuleList(string id, string parentId, string appId)
        {
            List<SelectListItem> model = new List<SelectListItem>();
            model.Add(new SelectListItem { Value = string.Empty, Text = "Select Module", Selected = true });
            model.AddRange(db.Modules.Where(m => m.AppId == appId && m.Id != id).Select(m => new SelectListItem { Value = m.Id, Text = m.ModuleName, Selected = parentId == null ? false : parentId == m.Id }).ToList());

            return PartialView("_ModuleSelectList", model);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}